home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / cfuncs.zip / DWEDIT.C < prev    next >
C/C++ Source or Header  |  1992-01-15  |  10KB  |  389 lines

  1. #include <conio.h>
  2. #include <alloc.h>
  3. #include <stdio.h>
  4. #include <funcs.h>
  5.  
  6. unsigned MaxList = 500;
  7. extern void (*idle_func) (void);
  8.  
  9. /*----------------------dwedit-----------------------------*/
  10. /*             DOUBLE WINDOW EDIT                        */
  11. /*                               */
  12. /*DESCRIPTION: Sets up two windows on the console and        */
  13. /*      allows for data entry in the windows and number       */
  14. /*    checking.                       */
  15. /*                               */
  16. /*INPUT:                                                   */
  17. /*     filename - name for output file               */
  18. /*     width1   - width of window 1               */
  19. /*     width2   - width of window 2               */
  20. /*     length   - length of windows                        */
  21. /*     fcheck1 & fcheck2  - functions that check for a     */
  22. /*          a valid number.  Return 1 for pass, 0    */
  23. /*          for fail.  Passing NULL means no check.  */
  24. /*                               */
  25. /* USES: Frame, OnCursor, OffCursor               */
  26. /*---------------------------------------------------------*/
  27.  
  28. dwedit( const char *filename, char action, int width1, int width2,
  29.     int length, int (*fcheck1) (const char *),
  30.     int (*fcheck2) (const char *))
  31.  
  32. {
  33.  
  34.   int i, CurWin = 0, key = '', LinMod, done = 1, MovWin = 0;
  35.   int y, cursorx = 0, cursory = 0, index = 0, count = 0, move = 1;
  36.   int TopWinElt = 0, OldWin= 3, movey =0, movex= 0, tmp;
  37.   int x[2], width[2];
  38.   int (*fcheck[2]) (const char *);
  39.   FILE *fp;
  40.   char *list[2];
  41.   char buffer[1300], *ptr;
  42.   FrameDataType Fr;
  43.   void DisplayDweditWindows(char *list[2], int TopOfWin, int X1,
  44.         int X2, int Y, int W1, int W2, int Length, int Count);
  45.   void BuildWeditScreen();
  46.  
  47. /* end data declarations  */
  48.  
  49.   if (action == 'X')
  50.     return(0);
  51.  
  52.  /*   initialize vars  */
  53.   width[0] = width1;
  54.   width[1] = width2;
  55.   y = 14 - (length / 2);
  56.   x[0] = 36 - ((width[0] + width[1]) /2);
  57.   x[1] = x[0] + width[0] + 3;
  58.   fcheck[0] = fcheck1;
  59.   fcheck[1] = fcheck2;
  60.  
  61.   /*------ALLOCATE MEMORY FOR DATA------*/
  62.   if((list[0] = (char *)calloc(MaxList, width1+1)) == NULL ||
  63.      (list[1] = (char *)calloc(MaxList, width2+1)) == NULL)
  64.   {
  65.      message(2, "Not Enough Memory Available");
  66.      if(list[0]!=NULL)
  67.     free(list[0]);
  68.      return(0);
  69.   }
  70.  
  71. /*---------------READ IN DATA FROM FILE ------*/
  72.   if (action != 'O' && action != 'A')
  73.     action = 'A';
  74.   if (action == 'A')
  75.   {
  76.     if ((fp = fopen(filename, "rt")) != NULL)
  77.     {
  78.       while (fgets(buffer, 60, fp) != NULL)
  79.       {
  80.      memcpy(list[0]+count*(width1+1), buffer, width1);
  81.      memcpy(list[1]+(count++)*(width2+1), buffer + width1 + 1, width2);
  82.       }
  83.       TopWinElt = (count /length) * length;
  84.       cursory = count % length;
  85.       index = TopWinElt + cursory;
  86.       fclose(fp);
  87.     }
  88.     else  /* file not found */
  89.     action = 'O';
  90.   } /* append */
  91.  
  92.   BuildWeditScreen();
  93.  
  94.  /*----------DISPLAY FRAME ---------*/
  95.   Fr.clear = 2;
  96.   Frame(&Fr);
  97.  
  98.   Fr.X = x[0]-1; Fr.Y = y-1;
  99.   Fr.L = length+2; Fr.W = width[0]+2;
  100.   Fr.F = CYAN; Fr.B = BLACK;
  101.   Fr.BorderType = 0;
  102.   Frame(&Fr);
  103.   Fr.X = x[1]-1; Fr.F = BROWN;
  104.   Fr.W = width[1]+2;
  105.   Frame(&Fr);
  106.   OnCursor();
  107.  
  108. /*---------------MAIN LOOP--------------*/
  109.   while (done != 0)
  110.   {
  111. /*----------- DISPLAY STATS ------------*/
  112.     if (move != 0)
  113.     {
  114.       WriteAt(20, 15, YELLOW, BLACK, ptr="%-4d", index+1);
  115.       Write(ptr, count);
  116.       Write(ptr, MaxList-count);
  117.       move = 0;
  118.     }
  119.  
  120. /*--------- DISPLAY WINDOWS ----------------*/
  121.     if(OldWin != TopWinElt)
  122.     DisplayDweditWindows(list, TopWinElt, x[0], x[1], y, width[0], width[1],
  123.              length, count);
  124.     gotoxy(x[CurWin] + cursorx, y+cursory);
  125.     OldWin = TopWinElt;
  126.     if(CurWin==0)
  127.       textattr(CYAN);
  128.     else
  129.       textattr(BROWN);
  130.  
  131. /*------------- GET KEY STROKE ----------------*/
  132.     key = GetIdleCh();
  133.  
  134.     if (key == '\0')
  135.       key = 256 + getch();
  136.     else if (key >= 32 && key <= 256)
  137.     {
  138.     putch(key);
  139.     *(list[CurWin]+index*(width[CurWin]+1) +cursorx) = key;
  140.     movex++;
  141.     }
  142.  
  143.     switch (key){
  144.     case 13:   /* Enter asc 13       */
  145.       MovWin++;
  146.       break;
  147.     case 324:   /* F10 asc 0, 68 - save data & exit  */
  148.     case 323:   /* F9 asc 0, 67  - save data */
  149.         gettext(29, 23, 46, 24, buffer);
  150.         //fflush(&buffer);
  151.         WriteAt(29, 24, YELLOW, DARKGRAY, " Saving Data ... ");
  152.         /*-----------WRITE TO THE FILE-------------*/
  153.         if ((fp = fopen(filename, "wt")) != NULL)
  154.         for (i = 0; i < count; i++)
  155.             if(!blanks(list[0]+i*(width[0]+1)) || !blanks(list[1]+i*(width[1]+1)))
  156.                 fprintf( fp, "%*s %*s\n",
  157.                     width[0],
  158.                     list[0]+i*(width[0]+1),
  159.                     width[1],
  160.                     list[1]+i*(width[1]+1));
  161.         fclose(fp);
  162.         puttext(29, 23, 46, 24, buffer);
  163.  
  164.         if(key==324)  /* F10 - done */
  165.             done=0;
  166.         break;
  167.     case 322: /* F8 - abort */
  168.         idle_func = nothing;
  169.         i = toupper(message(0," Recent Modifications Will Be Lost, Are You Sure? (Y/N)"));
  170.         if(i=='Y')
  171.             done = 0;
  172.         break;
  173.     case 328:   /* Up arrow asc 0, 72    */
  174.       --movey;
  175.       break;
  176.     case 336:   /* Down arrow asc 0, 80  */
  177.       movey++;
  178.       break;
  179.     case 333:   /* Right arrow asc 0, 77 */
  180.       movex++;
  181.       break;
  182.     case 331:   /* Left arrow asc 0, 75  */
  183.       movex--;
  184.       break;
  185.     case 9:   /* Tab key */
  186.        MovWin++;
  187.        break;
  188.     case 271: /* backtab key */
  189.        MovWin--;
  190.        break;
  191.     case 327:   /* Home asc 0, 71        */
  192.       movey = -index;
  193.       movex = -cursorx;
  194.       break;
  195.     case 335:   /* End Key asc 0, 79        */
  196.       movey = count - index -1;
  197.       movex = -cursorx;
  198.       break;
  199.     case 329:   /* Pageup Key asc 0, 73        */
  200.       movey -= length;
  201.       break;
  202.     case 337:   /* Page down Key asc 0, 81        */
  203.       movey += length;
  204.       break;
  205.     case  8:   /* BackSpace asc 8    */
  206.       if(cursorx != 0) {
  207.          gotoxy(x[CurWin] + cursorx-1, y + cursory);
  208.          for(i = cursorx-1; i< width[CurWin] -1; ++i) {
  209.         *(list[CurWin]+index*(width[CurWin]+1)+i) = *(list[CurWin]+index*(width[CurWin]+1)+i+1);
  210.         putch(*(list[CurWin]+index*(width[CurWin]+1)+i)); }
  211.          *(list[CurWin]+index*(width[CurWin]+1)+i) = ' ';
  212.          putch(' ');
  213.          movex--; }
  214.       break;
  215.     case 339:  /*  Del key  asc 0, 83*/
  216.       for(i = cursorx; i< width[CurWin] -1; ++i) {
  217.          *(list[CurWin]+index*(width[CurWin]+1)+i) = *(list[CurWin]+index*(width[CurWin]+1)+i+1);
  218.          putch(*(list[CurWin]+index*(width[CurWin]+1)+i)); }
  219.       *(list[CurWin]+index*(width[CurWin]+1)+i) = ' ';
  220.       putch(' ');
  221.       break;
  222.     default:
  223.       break;
  224.     } /* end switch */
  225.  
  226. /*------------- PROCESS KEY STROKE ---------------------*/
  227.     if(movex + cursorx >= width[CurWin])
  228.        MovWin++;
  229.     if(movex + cursorx < 0)
  230.        MovWin--;
  231.  
  232.  /*---------- CHECK FOR A VALID NUMBER --------*/
  233.     if (fcheck[CurWin] != NULL)
  234.     {
  235.        if (key >= 48 && key <= 125)  LinMod = 1;
  236.        if (LinMod && (movey || MovWin))
  237.        {   if( !CkBeep(fcheck[CurWin](list[CurWin]+index*(width[CurWin]+1))))
  238.         movex = movey = MovWin = 0;
  239.        LinMod = 0;
  240.        }
  241.     } /* end check */
  242.  
  243.     if(MovWin < 0 && CurWin == 0) movey--;
  244.     if(MovWin > 0 && CurWin == 1) movey++;
  245.  
  246.     if(movey + cursory > length -1)
  247.     {   /* past bottom of window */
  248.        if((tmp = (index + movey) / length * length ) < MaxList)
  249.        {
  250.         TopWinElt = tmp;
  251.         movey = -(cursory - (cursory+movey) % length);
  252.        }
  253.     }
  254.     else if(movey + cursory < 0)    /* past top of window */
  255.        if(index + movey >= 0) {      /* check for top of list */
  256.       TopWinElt = (index + movey)/length *length;
  257.       movey = -(cursory - (index + movey) % length); }
  258.     else
  259.       movey = 0;
  260.  
  261.     if(index +1 > count && !blanks(list[CurWin]+index*(width[CurWin]+1))) {
  262.        count = index +1;
  263.        ++move; }
  264.  
  265.     cursorx += movex;
  266.     if(TopWinElt+cursory+movey< MaxList)
  267.     cursory += movey;
  268.     else /* tried to go past max limit */
  269.     {
  270.     Beep(250, 2); delay(100);
  271.     Beep(250, 2); delay(100);
  272.     Beep(250, 2);
  273.     }
  274.  
  275.     CurWin += MovWin;
  276.  
  277.     if(movey || OldWin != TopWinElt) ++move;
  278.     if(CurWin < 0) CurWin = 1;
  279.     if(CurWin > 1) CurWin = 0;
  280.     if(MovWin < 0) cursorx = width[CurWin] -1;
  281.     if(MovWin > 0) cursorx = 0;
  282.  
  283.     MovWin = movex = movey = 0;
  284.  
  285.     index = TopWinElt + cursory;
  286.  
  287.   } /* end while */
  288.  
  289.   /*------FREE MEMORY-------*/
  290.   free(list[0]);
  291.   free(list[1]);
  292.  
  293.   OffCursor();
  294.  
  295.   return(1);
  296.  
  297. }  /* end wedit */
  298.  
  299. void BuildWeditScreen()
  300. {
  301.  
  302.   FrameDataType Fr;
  303.  
  304.   NewClear (CYAN, BLACK);
  305.  
  306.  
  307.   Fr.clear = 2;
  308.   Frame (&Fr);
  309.  
  310.   Fr.X = 49; Fr.Y =  8;
  311.   Fr.L = 13; Fr.W = 31;
  312.   Fr.F = GREEN; Fr.B = BLACK;
  313.   Fr.BorderType = 1;
  314.   Fr.txt[0] = "  Use Cursor Keys to Edit";
  315.   Fr.txt[2] = " Home  .. Top of List";
  316.   Fr.txt[3] = " End   .. Bottom of List";
  317.   Fr.txt[4] = " Up    .. Move Cursor Up";
  318.   Fr.txt[5] = " Down  .. Move Cursor Down";
  319.   Fr.txt[6] = " Left  .. Move Cursor Left";
  320.   Fr.txt[7] = " Right .. Move Cursor Right";
  321.   Fr.txt[8] = " PgUp  .. Goto Prev Screen";
  322.   Fr.txt[9] = " PgDn  .. Goto Next Screen";
  323.   Frame(&Fr);
  324.  
  325.   Fr.X =  3;    Fr.Y =  7;
  326.   Fr.L =  6;    Fr.W = 22;
  327.   Fr.F = GREEN; Fr.B = BLACK;
  328.   Fr.BorderType = 1;
  329.   Fr.txt[0] = "      OPTIONS";
  330.   Fr.txt[1] = " F10 to Save & Exit";
  331.   Fr.txt[2] = " F9 to Save";
  332.   Fr.txt[3] = " F8 to Abort";
  333.   Frame ( &Fr );
  334.  
  335.  
  336.   Fr.X = 26;    Fr.Y =  2;
  337.   Fr.L =  3;    Fr.W = 24;
  338.   Fr.F = GREEN; Fr.B = BLACK;
  339.   Fr.BorderType = 2;
  340.   Fr.txt[0] = "  Data Entry Screen ";
  341.   Frame ( &Fr );
  342.  
  343.  
  344.   Fr.X =  2;     Fr.Y = 14;
  345.   Fr.L =  5;     Fr.W = 24;
  346.   Fr.F = YELLOW; Fr.B = BLACK;
  347.   Fr.BorderType = 1;
  348.   Fr.txt[0] = " Current Number: ";
  349.   Fr.txt[1] = "          Count: ";
  350.   Fr.txt[2] = "    Memory Left: ";
  351.   Frame( &Fr );
  352.  
  353. }
  354.  
  355. void DisplayDweditWindows(char *list[2], int TopOfWin, int X1,
  356.         int X2, int Y, int W1, int W2, int Length, int Count)
  357. {
  358.  
  359.   int i;
  360.  
  361.   if (TopOfWin < 0) TopOfWin = 0;
  362.  
  363.   for (i = TopOfWin; i < TopOfWin + Length; i++)
  364.       if(i<Count)
  365.       {     /* data exists, so just print it */
  366.     WriteAt(X1, Y+i-TopOfWin, CYAN, BLACK, list[0]+i*(W1+1));
  367.     WriteAt(X2, Y+i-TopOfWin, BROWN, BLACK, list[1]+i*(W2+1));
  368.       }
  369.       else
  370.       {
  371.     /*  write spaces in window */
  372.     WriteAt(X1, Y+i-TopOfWin, CYAN, BLACK, "%*s", W1, " ");
  373.     WriteAt(X2, Y+i-TopOfWin, BROWN, BLACK, "%*s", W2, " ");
  374.       }
  375. }
  376.  
  377.  
  378. int Chk(const char *num)
  379. { int result;
  380.   result = strcmp(num, "dd");
  381.   return(result==0);
  382. }
  383. /*
  384. void main(void)
  385. {
  386.   dwedit("DATA.fil", AddAppend("data.fil"), 9, 2, 12, CheckNum, Chk);
  387. }
  388.  
  389.  */